package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.person;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ctetin.expandabletextviewlibrary.ExpandableTextView;
import com.ctetin.expandabletextviewlibrary.app.StatusType;
import com.kaiwukj.android.mcas.http.imageloader.glide.GlideArms;
import com.kaiwukj.android.mcas.utils.McaUtils;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicResult;
import com.kaiwukj.android.ufamily.mvp.ui.adapter.NineGridIvAdapter;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

public class PersonDynamicAdapter extends BaseQuickAdapter<DynamicResult, BaseViewHolder> {
    public PersonDynamicAdapter() {
        super(R.layout.item_dynamic_person, new ArrayList<>());
        addChildClickViewIds(R.id.iv_video_play);
    }

    @Override
    public long getItemId(int position) {
        return super.getItem(position).getId();
    }

    public int getPositionById(long id) {
        for (int i = 0; i < getData().size(); i++) {
            if (getData().get(i).getId() == id) {
                return i;
            }
        }
        return -1;
    }

    public void deleteById(long id) {
        int pos = getPositionById(id);
        if (pos >= 0)
            removeAt(pos);

    }

    public long getLastItemId() {
        if (getData().size() == 0) return 0;
        return getItemId(getData().size() - 1);
    }

    @Override
    protected void convert(BaseViewHolder helper, DynamicResult item) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(TimeUtils.string2Date(item.getCreateTime()));
        helper.setText(R.id.tv_day, String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
        helper.setText(R.id.tv_month, String.format("%s月", calendar.get(Calendar.MONTH) + 1));
        ExpandableTextView expandableTextView = helper.getView(R.id.tv_note);
        expandableTextView.setContent(item.getDynamicContent());
//        helper.setText(R.id.tv_note, item.getDynamicContent());
        expandableTextView.setExpandOrContractClickListener(type -> {
            if (type == StatusType.STATUS_CONTRACT) {
                if (mOnContractEventListener != null) {
                    mOnContractEventListener.onContract(helper.getAdapterPosition());
                }
            }
        });

        ViewGroup containerMedia = helper.getView(R.id.container_media);
        NineGridView containerImage = helper.getView(R.id.container_image);
        ViewGroup containerVideo = helper.getView(R.id.container_video);

        if (StringUtils.isEmpty(item.getImages()) && StringUtils.isEmpty(item.getVideo())) {
            containerMedia.setVisibility(View.GONE);
        } else {
            containerMedia.setVisibility(View.VISIBLE);
            if (!StringUtils.isEmpty(item.getVideo())) {
                //存在视频
                ImageView ivVideoCover = helper.getView(R.id.iv_video_cover);
                GlideArms.with(getContext())
                        .load(item.getImages())
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(ivVideoCover);
                containerImage.setVisibility(View.GONE);
                containerVideo.setVisibility(View.VISIBLE);
                ivVideoCover.setOnClickListener(v -> ARouter.getInstance().build(ARouterUrlKt.VideoPlay)
                        .withString("videoUrl", item.getVideo())
                        .withString("coverUrl", item.getImages())
                        .withString("title", item.getDynamicContent())
                        .navigation(getContext()));
            } else {
                //图文
                List<String> imageList = Arrays.asList(item.getImages().split(","));
                initGroupImageAdapter(imageList, containerImage);
                containerVideo.setVisibility(View.GONE);
                containerImage.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * @param imageList
     * @param mImageGroup
     */
    private void initGroupImageAdapter(List<String> imageList, NineGridView mImageGroup) {
        ArrayList<ImageInfo> imageInfo = new ArrayList<>();
        for (int i = 0; i < imageList.size(); i++) {
            ImageInfo info = new ImageInfo();
            info.setThumbnailUrl(imageList.get(i));
            info.setBigImageUrl(imageList.get(i));
            imageInfo.add(info);
        }
        //单张图片的大小
        NineGridIvAdapter mGridIvAdapter = new NineGridIvAdapter(getContext(), imageInfo);
        mImageGroup.setSingleImageSize(McaUtils.getScreenWidth(getContext()) / 2);
        mImageGroup.setAdapter(mGridIvAdapter);
    }

    private OnContractEventListener mOnContractEventListener;

    public void setOnContractEventListener(OnContractEventListener listener) {
        mOnContractEventListener = listener;
    }

    public interface OnContractEventListener {
        void onContract(int position);
    }
}
